import {
  societyList,
  newsList,
  swiperList,
  trainingbanner
} from '../../apis/index';
import {
  splicing,
  formatTime
} from '../../utils/util';
const app = getApp();

Page({
  data: {
    swiperList: [],
    societyList: [],
    newsList: [],
    pageNum: 1,
    isRequest: false,
    isHaveMore: true,
    isClickMore: false
  },

  onLoad() {
    this.getSwiperList();
    this.getSocietyList();
    this.getNewsList(1, 5);
  },

  onShareAppMessage() {
    return {
      title: '北京市移民及出入境服务行业协会',
      path: '/pages/start/index',
      imageUrl: '/images/logo.png'
    }
  },

  onReachBottom() {
    if (this.data.isHaveMore && this.data.isClickMore) {
      this.getNewsList(this.data.pageNum + 1, 5, 'load');
    };
  },

  // 新闻列表
  getNewsList(pageNum, pageSize, type) {
    if (this.data.isRequest) return
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    this.setData({
      isRequest: true
    });
    newsList(pageNum, pageSize).then(res => {
      wx.hideLoading();
      this.setData({
        isRequest: false
      });
      if (res.data.code === 200) {
        this.setData({
          pageNum: pageNum
        });
        let {
          rows,
          total
        } = res.data;
        rows.forEach(v => {
          v.image = splicing(v.image);
          v.dateTime = formatTime(v.dateTime);
        });
        if (type === 'load') {
          rows.forEach(v => {
            this.data.newsList.push(v);
          });
          this.setData({
            newsList: this.data.newsList
          });
        } else {
          this.setData({
            newsList: rows
          });
        };

        if (this.data.newsList.length === total) {
          this.setData({
            isHaveMore: false
          });
        };
      };
    });
  },

  // 协会相关
  getSocietyList() {
    societyList().then(res => {
      if (res.data.code === 200) {
        let {
          rows
        } = res.data;
        this.setData({
          societyList: rows
        });
      };
    });
  },

  // 轮播图
  getSwiperList() {
    trainingbanner(1).then(res => {
      if (res.data.code === 200) {
        let {
          rows
        } = res.data;
        rows.forEach(v => {
          v.image = splicing(v.image);
        });
        this.setData({
          swiperList: rows
        });
      };
    });
  },

  loadMore() {
    if (this.data.isHaveMore && !this.data.isClickMore) {
      this.getNewsList(this.data.pageNum + 1, 5, 'load');
      this.setData({
        isClickMore: true
      });
    };
  },

  // 协会详情
  societyDetails(e) {
    let {
      id,
      ipage
    } = e.currentTarget.dataset;
    let str = `/pages/aboutSociety/index?societyId=${id}`
    if(ipage){
        str = '/' + ipage
    }
    wx.navigateTo({
        url: str
    });        
  },

  // 新闻详情
  newsDetails(e) {
    let {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/newsDetails/index?newsId=${id}`
    });
  },

  clickSwiper(e) {
    let {
      url
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/' + url
    });
  }
})